package tw.BBT.BasalBodyTemperature.Listeners.TabPageContents.Page_Main;

import tw.BBT.BasalBodyTemperature.BasalBodyTemperature;
import tw.BBT.BasalBodyTemperature.R;
import tw.BBT.BasalBodyTemperature.Components.Common.DBhelper;
import tw.BBT.BasalBodyTemperature.Components.Common.PopUp_TextPicker;
import tw.BBT.BasalBodyTemperature.Components.Common.TemperatureStatisticDraw;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class BTN_Start_Listener implements OnClickListener {

	BasalBodyTemperature BBT;
	TemperatureStatisticDraw TSD ;
	
	public BTN_Start_Listener(BasalBodyTemperature BBT,SurfaceView TemperatureStatistic) {
		this.BBT = BBT;
		TSD = new TemperatureStatisticDraw(BBT,TemperatureStatistic);
	}

	/* Listener for ReNameDiagram: 
	 * (a) button(PopUp_TextPicker_Apply)
	 * (b) button(PopUp_TextPicker_Cancel)
	 */
	class GetTemperature_Listener implements View.OnClickListener {

		AlertDialog GetTemperatureDia;
		PopUp_TextPicker GetTemperatureDiaView;
		TemperatureStatisticDraw TSD ;
		
		GetTemperature_Listener(PopUp_TextPicker GetTemperatureDiaView,AlertDialog GetTemperatureDia,TemperatureStatisticDraw TSD) {
			this.GetTemperatureDia = GetTemperatureDia;
			this.GetTemperatureDiaView = GetTemperatureDiaView;
			this.TSD = TSD;
		}
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			long id = v.getId();
			if(id == R.id.PopUp_TextPicker_Apply){
				
				DBhelper dbh = new DBhelper(BBT);
		        SQLiteDatabase wrdb = dbh.getWritableDatabase();
		        
		        //Insert
		        ContentValues args = new ContentValues();
		         args.put("Temperature",Float.parseFloat(GetTemperatureDiaView.getTextPicker_TextInput().toString()));
		         long rowid = wrdb.insert("BBTData",null,args);
		         wrdb.close();
				
				GetTemperatureDia.cancel();
			}	else if(id == R.id.PopUp_TextPicker_Cancel){
				GetTemperatureDia.cancel();
			}			
			
			try {
				TSD.Draw(TemperatureStatisticDraw.DrawAction.StatisticDraw);
			} catch(Exception e) { }
			
		}
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		// AlertDialog Instance
		final AlertDialog GetTemperatureDia = new AlertDialog.Builder(BBT).create();

		// Create view for AlertDialog
		PopUp_TextPicker GetTemperatureDiaView = new PopUp_TextPicker(BBT);
		
		// Definition things
		GetTemperatureDiaView.setTextPicker_Message("Today Temperature: ");
		GetTemperatureDiaView.setTextPicker_TextInput("36.6");
		
		// Set Listener for AlertDialog
		GetTemperature_Listener GetTemperature_L = new GetTemperature_Listener(GetTemperatureDiaView, GetTemperatureDia,TSD);
		GetTemperatureDiaView.setListener(GetTemperature_L);
		
		// Show AlertDialog
		GetTemperatureDia.setView(GetTemperatureDiaView.getView());
		GetTemperatureDia.show();
	}
}
